﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace Itasa.API
{
    public class Sub : IComparable, IComparable<Sub>
    {

        internal long ID { get; private set; }
        internal String Name { get; private set; }
        internal String Version { get; private set; }
        internal long ShowID { get; private set; }
        internal DateTime SubmitDate { get; private set; }
        internal List<DateTime> DownloadDate { get; private set; }
        internal SubInfo SubInfo { get; set; }

        internal Sub(XmlNode sub_node)
        {
            DownloadDate = new List<DateTime>();
            foreach (XmlNode sd in sub_node.ChildNodes)
            {
                if (sd.InnerText != null)
                {
                    if (sd.Name == "show_id") ShowID = long.Parse(sd.InnerText);
                    if (sd.Name == "id") ID = long.Parse(sd.InnerText);
                    if (sd.Name == "name") Name = sd.InnerText;
                    if (sd.Name == "version") Version = sd.InnerText;
                    if (sd.Name == "submit_date") SubmitDate = DateTime.Parse(sd.InnerText);
                    if (sd.Name == "download_date" && sd.InnerText != "") DownloadDate.Add(DateTime.Parse(sd.InnerText));
                }

            }
        }

        internal String Season()
        {
            String title = (String)this.Name.Clone();
            while (title.EndsWith(" ")) title = title.Remove(title.Length - 1);
            int i = title.LastIndexOf("Stagione");
            if (i == -1)
            {
                i = title.LastIndexOf("Completa");
                if (i == -1)
                {
                    i = title.LastIndexOf('x');
                    if (i == -1 || i < title.Length - 3)
                    {
                        return "0";//null; NESSUNA STAGIONE
                    }
                    else
                    {
                        int j = i;
                        while (title[i] != ' ') i--;
                        i++;
                        //j--;
                        String tmp = title.Substring(i, j - i);
                        while (tmp.StartsWith("0")) tmp = tmp.Remove(0, 1);
                        return tmp;
                    }
                }
                else
                {
                    String tmp = title.Remove(i - 1);
                    int rl = tmp.LastIndexOf(' ');
                    tmp = tmp.Remove(0, rl);
                    while (tmp.StartsWith("0") || tmp.StartsWith(" ")) tmp = tmp.Remove(0, 1);
                    return tmp;
                }
            }
            else
            {
                String tmp = title.Remove(0, i + 9);
                int rl = tmp.IndexOf(' ');
                tmp = tmp.Remove(rl);
                while (tmp.StartsWith("0")) tmp = tmp.Remove(0, 1);
                return tmp;
            }
        }

        internal void AddDownloadDate(List<DateTime> dt)
        {
            foreach (DateTime d in dt) DownloadDate.Add(d);
        }

        internal void AddInfo(SubInfo si)
        {
            this.SubInfo = si;
        }

        public override bool Equals(object obj)
        {
            if (obj is Sub) if (this.ID.Equals((obj as Sub).ID)) return true;
            return false;
        }

        public int CompareTo(object obj)
        {
            if (obj is Sub) return this.SubmitDate.CompareTo((obj as Sub).SubmitDate);
            return 0;
        }

        public override int GetHashCode()
        {
            return (int)this.ID;
        }

        public int CompareTo(Sub other)
        {
            if (other == null) throw new ArgumentNullException("other");
            return this.SubmitDate.CompareTo(other.SubmitDate);
        }
    }
}
